<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\form\util\control\dctrl\items;

use builder\helper\H5;
use builder\helper\Url;
use yii\base\InvalidArgumentException;
use yii\helpers\ArrayHelper;

/**
 * 文件控件
 * @author cleverstone
 * @since ym2.0
 */
class File extends Dcontrol
{
    /**
     * @var array
     */
    private $_options = [
        'control' => 'file',
        'label' => '',
        'required' => false,
        'comment' => '',
        'style' => ['width' => '80px', 'height' => '80px'],
        'class' => '',
        'minWidth' => '',

        'max' => 1,
        'default' => [],
        'scenario' => [],
    ];

    /**
     * 单元格最小宽度
     * @param integer|string $minWidth
     * @return $this
     */
    public function minWidth($minWidth)
    {
        if (is_numeric($minWidth)) {
            $minWidth .= 'px';
        }

        $this->_options['minWidth'] = $minWidth;
        return $this;
    }

    /**
     * 最大数量
     * @param int $count
     * @return $this
     */
    public function max($count)
    {
        $this->_options['max'] = (int)$count;
        return $this;
    }

    /**
     * 控件标签
     * @param string $label
     * @return $this
     */
    public function label($label)
    {
        $this->_options['label'] = (string)$label;
        return $this;
    }

    /**
     * 默认值
     * @param array|string $default
     * @return $this
     */
    public function defaultValue($default)
    {
        $default = (array)$default;
        foreach ($default as $index => $path) {
            if (!is_string($path)) {
                throw new InvalidArgumentException('参数类型错误!');
            }

            $default[$index] = Url::pathToArray($path);
        }

        $this->_options['default'] = $default;
        return $this;
    }

    /**
     * 是否必填
     * @param bool $required
     * @return $this
     */
    public function required($required = true)
    {
        $this->_options['required'] = (boolean)$required;
        return $this;
    }

    /**
     * 字段注释
     * @param string $comment
     * @return $this
     */
    public function comment($comment)
    {
        $this->_options['comment'] = (string)$comment;
        return $this;
    }

    /**
     * CSS样式
     * @param array|string $style
     * @return $this
     */
    public function style($style)
    {
        if (is_string($style)) {
            $style = H5::cssStyleToArray($style);
        }

        $style = ArrayHelper::merge($this->_options['style'], $style);
        $this->formatStyle($style);
        $this->_options['style'] = $style;

        return $this;
    }

    /**
     * UI
     * @param array|string $class
     * @return $this
     */
    public function uiClass($class)
    {
        $this->formatUiClass($class);
        $this->_options['class'] = $class;

        return $this;
    }

    /**
     * 图片上传场景
     * @return $this
     */
    public function scenarioImage()
    {
        $this->_options['scenario']['image'] = 'IMAGE';
        return $this;
    }

    /**
     * 其他上传场景
     * @return $this
     */
    public function scenarioOther()
    {
        $this->_options['scenario']['other'] = 'OTHER';
        return $this;
    }

    /**
     * 音频上传场景
     * @return $this
     */
    public function scenarioAudio()
    {
        $this->_options['scenario']['audio'] = 'AUDIO';
        return $this;
    }

    /**
     * 视频上传场景
     * @return $this
     */
    public function scenarioVideo()
    {
        $this->_options['scenario']['video'] = 'VIDEO';
        return $this;
    }

    /**
     * @return array
     * @throws \Exception
     */
    public function _toArray()
    {
        $options = $this->_options;
        $options['scenario'] = array_values($options['scenario']);
        if (is_array($options['style'])) {
            $this->formatStyle($options['style']);
        }

        if (count($options['default']) > $options['max']) {
            throw new \Exception('文件数量大于允许的最大数量！');
        }

        return $options;
    }
}